package com.itjiange.service.impl;

import com.itjiange.dao.UserDao;
import com.itjiange.dao.impl.UserDaoImpl;
import com.itjiange.pojo.User;
import com.itjiange.pojo.UserRegisterDto;
import com.itjiange.service.UserService;
import com.mysql.cj.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * 用户登录
 *
 * @author ItJiange
 * @date 2023/10/16
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return 登录是否成功
     */
    public boolean userLogin(String username, String password) {
        // 1. 校验
        if (StringUtils.isNullOrEmpty(username)) {
            System.out.println("用户名为空");
            return false;
        }
        if (StringUtils.isNullOrEmpty(password)) {
            System.out.println("密码为空");
            return false;
        }
        // 2. 查询数据
        User user = userDao.getUser(username, password);

        return user != null;
    }

    @Override
    public boolean userRegister(UserRegisterDto userRegisterDto) {
        String username = userRegisterDto.getUsername();
        String userpassword = userRegisterDto.getUserPassword();
        String repeatPassword = userRegisterDto.getRepeatPassword();
        String emailaddress = userRegisterDto.getEmailAddress();
        String hobby = userRegisterDto.getHobby();
        if (StringUtils.isNullOrEmpty(username)) {
            System.out.println("用户名为空，注册失败");
            return false;
        }
        if (StringUtils.isNullOrEmpty(userpassword)) {
            System.out.println("用户密码为空，注册失败");
            return false;
        }
        if (StringUtils.isNullOrEmpty(repeatPassword)) {
            System.out.println("密码为空，注册失败");
            return false;
        }
        if (!repeatPassword.equals(userpassword)) {
            System.out.println("两个密码不一致");
            return false;
        }
        int flag = userDao.insertUser(userRegisterDto);
        return flag > 0;
    }

    @Override
    public List<User> userAll() {
        List<User> userList = userDao.selectAllUser();
        if (userList.isEmpty()) {
            System.out.println("查询结果为空");
            return Collections.emptyList();
        }
        return userList;
    }
}
